﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.NetworkInformation;
using System.IO;

/*
     This file is part of Plogger.

    Plogger is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Plogger is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Plogger.  If not, see <http://www.gnu.org/licenses/>.
  
 */

namespace PingLogger
{
    public class FileWriter
    {
        public string FileName;
        public List<string> PingResult = new List<string>();
        public List<string> TracertResult = new List<string>();

        private List<string> Result = new List<string>();

        public FileWriter()
        {
            this.Init();
        }

        public FileWriter(List<string> ping)
        {
            this.PingResult = ping;
            this.Init();
        }

        public FileWriter(List<string> ping, List<string> tracert)
        {
            this.PingResult = ping;
            this.TracertResult = tracert;
            this.Init();
        }

        public FileWriter(List<string> ping, string filename)
        {
            this.PingResult = ping;
            this.Init(filename);
        }

        public FileWriter(List<string> ping, List<string> tracert, string filename)
        {
            this.PingResult = ping;
            this.TracertResult = tracert;
            this.Init(filename);
        }

        private void Init(string file = @"PloggerLog.txt")
        {
            string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            this.FileName = String.Format("{0}\\{1}", @desktop, @file);
        }

        private void Merge()
        {
            if (this.TracertResult != null)
            {
                this.Result.AddRange(this.TracertResult);
                this.Result.Add("");
            }
            this.Result.AddRange(this.PingResult);
        }

        public void WriteToFile()
        {
            this.Merge();

            using (StreamWriter sw = new StreamWriter(this.FileName))
            {
                foreach (string s in this.Result)
                    sw.WriteLine(s);
            }
        }
    }
}
